import strutils

proc decode*(input: string): string = 
  var count = 1
  for x in tokenize(input, Digits):
    if x.isSep:
      count = x.token.parseInt
    else:
      result.add x.token[0].repeat count - 1
      result.add x.token
      count = 1

proc encode*(input: string): string = 
  if input.len == 0: return
  var
    i = 0
    count = 1
  while i < input.len - 1:
    if input[i] == input[i + 1]:
      inc(count)
    else:
      if count > 1: result.add($count)
      result.add(input[i])
      count = 1
    i.inc

  if count > 1: result.add($count)
  result.add(input[^1])
